So far, we've pretended that all files live in a single folder or
directory and it's the directory that your Python program is connected to.
So, your Python program doesn't need to specify a location for the file when opening it.
With our simulated files in the Runestone environment that worked fine,
but if you're ever running Python on a local machine it probably won't be enough for you.
So, let's see how you can have files organized
into folders and directories and still find them in your Python program.
The most people do organize their files into folders or
directories because otherwise you just have a hundreds
of files in one directory and you can't find them.
So, for example, here's a diagram showing a hierarchy of directories and files.
There's a top-level directory called myFiles and inside
that directory we have two other sub-directories, otherFiles, and allProjects.
AllProjects itself has two more sub-directories called myData and myProject.
Inside of myData, we've got a couple of files, one's called data2.txt.
In myProject, we've got data1.txt and myPythonProgram.py.
By convention, we put Python programs into files whose name ends in.py.
So a Python program, when we run it,
is automatically connected to the directory where it's invoked from.
We are invoking Python directly in
the Runestone book so we can't really see how that works,
but I can give you a way to think about it to use if and when you
do install Python on your own computer to run it locally.
In the open function,
you can pass just to filename as we've been doing
like that, open data1.txt for reading,
but we also can specify
a complete path that says where to find the file as well as the file's name.
So normally, we would use a relative path which specifies how to
get to another directory from the directory that you're currently connected to.
So suppose that we're running this myPythonProgram.py,
and we're running it from the directory myProject,
and we want to open data2.txt.
We can't just say open data2,
we have to tell it how to find it.
So, this is not going to work.
We have to instead say,
from the current directory, which is myProject,
you've got to go up a level to get to
all projects and the way to say that you go up a level is to say dot dot.
Dot dot says go to the containing or the parent directory,
within all projects, we have to go down,
descend into this sub-folder,
myData, and we got to go down there,
and now we've given the directions go up to the parent,
within that go down to myData and it looks like I forgot to capitalize the d,
Let me fix that, and then now we're in the right directory and now we can say data2.txt.
Everything else is just the same.
So that's called a relative path.
This part is called
the path and then we have the filename.
There's also a way to specify an absolute path,
that is absolute meaning.
Here's how to find this file on
the computer rather than relative to
the current directory that you're currently connected to.
When you do that, you'll have a path that begins with slash.
So it would be something like /User/presnick/myFiles,
and you'd have to give the whole path allProjects.
After allProjects, you would go to myData and finally the filename.
I don't recommend using these what are called absolute
paths because it makes your code and data not portable.
If I use the relative path,
and I take this entire set
of folders and I just copy it to someplace else on a different computer,
maybe somebody else's computer not /User/presnick,
and I can still find it.
So, generally, people prefer to use
these relative paths because it makes their code and data more portable.
You can transfer it to other computers.
Now, if you've only been running Python in the Runestone textbook,
you haven't had an occasion yet to use
these file paths when opening files and don't worry about it,
just make a mental note to come back to this video
or the corresponding page in the book when you are
executing in an environment with files grouped into directories. See you next time.